<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon32.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon16.ico?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="CG,">










<meta name="description" content="跟着Peter Shirley的著名Ray Tracing系列的前两本抄了一个ray tracer，教程非常简单，出效果很快，但是有些知识背景介绍较少，自己看下来觉得有必要仔细看的几个点做一下记录。">
<meta name="keywords" content="CG">
<meta property="og:type" content="article">
<meta property="og:title" content="RT with Peter Shirley">
<meta property="og:url" content="http://htelepathh.gitee.io/2019/10/09/RT-with-Peter-Shirley/index.html">
<meta property="og:site_name" content="OUTPUT">
<meta property="og:description" content="跟着Peter Shirley的著名Ray Tracing系列的前两本抄了一个ray tracer，教程非常简单，出效果很快，但是有些知识背景介绍较少，自己看下来觉得有必要仔细看的几个点做一下记录。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://htelepathh.gitee.io/2019/10/09/RT-with-Peter-Shirley/fixed_canbefix.jpg">
<meta property="og:image" content="http://htelepathh.gitee.io/2019/10/09/RT-with-Peter-Shirley/focus_lens.jpg">
<meta property="og:image" content="http://htelepathh.gitee.io/2019/10/09/RT-with-Peter-Shirley/defocus.jpg">
<meta property="og:image" content="http://htelepathh.gitee.io/2019/10/09/RT-with-Peter-Shirley/simulation.jpg">
<meta property="og:image" content="http://htelepathh.gitee.io/2019/10/09/RT-with-Peter-Shirley/hierarchies.jpg">
<meta property="og:updated_time" content="2019-10-17T09:47:30.343Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="RT with Peter Shirley">
<meta name="twitter:description" content="跟着Peter Shirley的著名Ray Tracing系列的前两本抄了一个ray tracer，教程非常简单，出效果很快，但是有些知识背景介绍较少，自己看下来觉得有必要仔细看的几个点做一下记录。">
<meta name="twitter:image" content="http://htelepathh.gitee.io/2019/10/09/RT-with-Peter-Shirley/fixed_canbefix.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":true},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://htelepathh.gitee.io/2019/10/09/RT-with-Peter-Shirley/">





  <title>RT with Peter Shirley | OUTPUT</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">OUTPUT</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">HTelepathH's blog</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
            留言
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://htelepathh.gitee.io/2019/10/09/RT-with-Peter-Shirley/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="HTelepathH">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/MOB.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="OUTPUT">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">RT with Peter Shirley</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-09T10:42:30+08:00">
                2019-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/ray-tracing/" itemprop="url" rel="index">
                    <span itemprop="name">ray tracing</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>跟着Peter Shirley的著名Ray Tracing系列的前两本抄了一个ray tracer，教程非常简单，出效果很快，但是有些知识背景介绍较少，自己看下来觉得有必要仔细看的几个点做一下记录。</p>
<a id="more"></a>
<img src="/2019/10/09/RT-with-Peter-Shirley/fixed_canbefix.jpg" title="final">
<h2 id="Defocus-Blur-Depth-Of-Field"><a href="#Defocus-Blur-Depth-Of-Field" class="headerlink" title="Defocus Blur / Depth Of Field"></a>Defocus Blur / Depth Of Field</h2><p>首先了解一下失焦模糊/景深的效果的成因。</p>
<img src="/2019/10/09/RT-with-Peter-Shirley/focus_lens.jpg" title="Figure 6.7 Gaussian lens equation[1]">
<p>$z’$表示在焦平面上的点到透镜中心的距离，$z$表示成像物体到透镜中心的距离，f为凸透镜焦距，有Gaussian lens equation：</p>
<script type="math/tex; mode=display">\frac{1}{z'}-\frac{1}{z}=\frac{1}{f}</script><p>显然$z=\infty$时,$z’=f$。</p>
<img src="/2019/10/09/RT-with-Peter-Shirley/defocus.jpg" title="Figure 6.11 Defocus Blur[1]">
<p>当对焦点选择在$z_f$位置时，对于该位置之后的点$z$，成像的焦点就在焦平面(成像平面)之后了，原本一个点的像会扩散为一个范围。同理，当$z$出现在$z_f$之前，光线会在焦平面(成像平面)之前聚焦，在成像平面上显示一个范围。失焦模糊$d_c$的范围大小与焦距$f$、对焦距离$z$和光圈大小$d_1$有关，结合相似，有：</p>
<script type="math/tex; mode=display">d_c=|\frac{d_1f(z-z_f)}{z(f+z_f)}|</script><p>效果的实现是模拟凸透镜成像，但是模拟有技巧。不模拟相机内部成像，在透镜平面采样几个起点，往对焦距离$z$的焦平面发射光线。因此，对于焦平面上的点，所有采样光线都会采到同一个点；而对于焦平面后的点，相当于经过焦平面的小孔，会采到该点和周围点，形成模糊；焦平面前的点与采样锥形相交，也成模糊。</p>
<img src="/2019/10/09/RT-with-Peter-Shirley/simulation.jpg" title="Simulation [2]">
<h2 id="Bounding-Volume-Hierarchies"><a href="#Bounding-Volume-Hierarchies" class="headerlink" title="Bounding Volume Hierarchies"></a>Bounding Volume Hierarchies</h2><p>使用axis-aligned bounding box(AABB)的Hierarchies来快速求交，这里使用了一个二叉树，每个像素从$O(N)$最好情况降到$O(logN)$。AABB的划分并不严格，左右子树的AABB可能重叠，两侧都计算后比较<code>hit_record</code>的先后。建树相对繁琐，为了使树尽量平衡，文中采用的是随机找一条axis，按照axis上的坐标排序，然后二分。<br>建好的BVH树里叶子结点均为能返回<code>hit_record</code>的实体，其他节点为<code>bvh_node</code>。因此在hit时，先用<code>bvh_node</code>的<code>aabb</code>快速求交判断，递归查找，直到叶子结点再生成<code>hit_record</code>。</p>
<h2 id="Perlin-Noise"><a href="#Perlin-Noise" class="headerlink" title="Perlin Noise"></a>Perlin Noise</h2><p>在理解的时候参考了[4]。书中的perlin noise稍有改进。用了三个随机坐标perm数组以及一个三维随机向量数组。在插值的时候，同时使用hermite cubic进行坐标插值，以及一个c[2][2][2]的随机向量数组来和偏移向量点积插值。随机向量数组的hash如下：<br><code>c[di][dj][dk] = ranvec[perm_x[(i + di) &amp; 255] ^ perm_y[(j + dj) &amp; 255] ^ perm_z[(k + dk) &amp; 255]]</code><br>由于单纯的perlin noise频率较为单一，因此加上了一层扰动，将远处的低频也加进来。<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">float</span> <span class="title">turb</span><span class="params">(<span class="keyword">const</span> vec3&amp; p, <span class="keyword">int</span> depth = <span class="number">7</span>)</span> <span class="keyword">const</span> </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">float</span> accum = <span class="number">0</span>;</span><br><span class="line">    vec3 temp_p = p;</span><br><span class="line">    <span class="keyword">float</span> weight = <span class="number">1.0</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; depth; ++i) &#123;</span><br><span class="line">        accum += weight * noise(temp_p);</span><br><span class="line">        weight *= <span class="number">0.5</span>;</span><br><span class="line">        temp_p *= <span class="number">2</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">fabs</span>(accum);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>最后在<code>texture</code>中得到结果后再加一层turb:<br><code>vec3(0.5 * (1 + sin(scale * p.x() + 10 * perlin.turb(p))))</code></p>
<h2 id="Program-structure"><a href="#Program-structure" class="headerlink" title="Program structure"></a>Program structure</h2><p><code>ray</code>较为简单。主要列出这里几何体的层次结构。</p>
<img src="/2019/10/09/RT-with-Peter-Shirley/hierarchies.jpg" title="Hierarchies [2]">
<p><code>hitable</code>主要完成hit(求交)的任务，并且返回<code>hit_record</code>。<code>hit_record</code>包含<code>material</code>的指针，法向量，交点坐标，<code>ray</code>上的t值，以及坐标的uv值。这里有”geometry”,”volumes”和”instance”三种概念上的分类。geometry在hit里按照几何规律，volumes按照phase_function，instance中用反变换来修正前两种。而<code>hitable_list</code>和<code>bvh_node</code>用来包装<code>hitable</code>数组。</p>
<p><code>material</code>主要决定ray在hit以后的scatter光线以及反射后的attenuation。另外不同材质有不同albedo参数，<code>dielectric</code>需要反射率，<code>metal</code>还可以加上fuzz扰动，对于需要材质颜色的<code>isotropic</code>和<code>diffuse_light</code>含有一个<code>texture</code>指针，用来根据uv获取颜色。</p>
<p><code>texture</code>主要根据uv求得颜色。可以是图片纹理也可以是几何纹理。</p>
<h2 id="References"><a href="#References" class="headerlink" title="References"></a>References</h2><p>[1]Physically Based Rendering, Third Edition<br>[2]Ray Tracing in a Weekend<br>[3]Ray Tracing_ The Next Week<br>[4]<a href="http://eastfarthing.com/blog/2015-04-21-noise/" target="_blank" rel="noopener">http://eastfarthing.com/blog/2015-04-21-noise/</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/CG/" rel="tag"># CG</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/09/20/Euler-Lagrange方程/" rel="next" title="Euler-Lagrange方程">
                <i class="fa fa-chevron-left"></i> Euler-Lagrange方程
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/12/19/Functor-Applicative-Monad以及Monoid/" rel="prev" title="Functor,Applicative,Monad以及Monoid">
                Functor,Applicative,Monad以及Monoid <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
      <div id="sidebar-dimmer"></div>
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/MOB.jpg" alt="HTelepathH">
            
              <p class="site-author-name" itemprop="name">HTelepathH</p>
              <p class="site-description motion-element" itemprop="description">Learning by doing.</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">25</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">15</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="99088577@qq.com" target="_blank" title="Mail">
                      Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/htelepathh" target="_blank" title="Gitee">
                      Gitee</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/htlpt" target="_blank" title="GitHub">
                      GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/u/6207125886" target="_blank" title="微博">
                      微博</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/huang-he-tu" target="_blank" title="知乎">
                      知乎</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Defocus-Blur-Depth-Of-Field"><span class="nav-number">1.</span> <span class="nav-text">Defocus Blur / Depth Of Field</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Bounding-Volume-Hierarchies"><span class="nav-number">2.</span> <span class="nav-text">Bounding Volume Hierarchies</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Perlin-Noise"><span class="nav-number">3.</span> <span class="nav-text">Perlin Noise</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Program-structure"><span class="nav-number">4.</span> <span class="nav-text">Program structure</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#References"><span class="nav-number">5.</span> <span class="nav-text">References</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-angle-double-left"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">HTelepathH</span>

  
</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          displayMath: [ ['$$', '$$']],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
